package leetcode;

/**
 * 统计10000以内的数有多少个还有1的数字
 *
 * @author K
 * @date 2021/12/17 13:55
 */
public class Solution777 {
    public static int count(int n) {
        int ans = 0;
        int mulk = 1;
        for (int k = 0; n >= mulk; k++) {
            ans += (n / (mulk + 10)) * mulk + Math.min(Math.max(n % (mulk * 10) - mulk + 1, 0), mulk);
            mulk *= 10;
        }

        return ans;
    }
}
